package com.example.a_java.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.a_java.common.Result;
import com.example.a_java.dto.BatchReminderDTO;
import com.example.a_java.dto.CollectionRecordDTO;
import com.example.a_java.dto.OverdueRepayDTO;
import com.example.a_java.dto.RepaymentReminderDTO;
import com.example.a_java.entity.LoanCollectionRecord;
import com.example.a_java.entity.LoanOverdue;
import com.example.a_java.service.OverdueService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 逾期管理控制器
 */
@Tag(name = "逾期管理", description = "逾期管理相关接口")
@RestController
@RequestMapping("/repayment/overdue")
@SecurityRequirement(name = "bearer-key")
public class OverdueController {

    @Autowired
    private OverdueService overdueService;

    /**
     * 获取逾期列表
     */
    @Operation(summary = "获取逾期列表")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('repayment:overdue:list')")
    public Result<Page<LoanOverdue>> list(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "贷款编号") @RequestParam(required = false) String loanNo,
            @Parameter(description = "客户姓名") @RequestParam(required = false) String customerName,
            @Parameter(description = "逾期天数") @RequestParam(required = false) String overdueDays,
            @Parameter(description = "处理状态") @RequestParam(required = false) String status) {

        IPage<LoanOverdue> page = overdueService.getOverdueList(
                new Page<>(pageNum, pageSize),
                loanNo,
                customerName,
                overdueDays,
                status);

        return Result.success((Page<LoanOverdue>) page);
    }

    /**
     * 发送还款提醒
     */
    @Operation(summary = "发送还款提醒")
    @PostMapping("/remind")
    @PreAuthorize("hasAuthority('repayment:overdue:remind')")
    public Result<Map<String, Object>> sendReminder(@RequestBody @Valid RepaymentReminderDTO reminderDTO) {
        Map<String, Object> result = overdueService.sendRepaymentReminder(reminderDTO);
        return Result.success(result);
    }

    /**
     * 批量发送还款提醒
     */
    @Operation(summary = "批量发送还款提醒")
    @PostMapping("/remind/batch")
    @PreAuthorize("hasAuthority('repayment:overdue:remind')")
    public Result<Map<String, Object>> batchSendReminder(@RequestBody @Valid BatchReminderDTO batchReminderDTO) {
        Map<String, Object> result = overdueService.batchSendReminder(batchReminderDTO);
        return Result.success(result);
    }

    /**
     * 催收登记
     */
    @Operation(summary = "催收登记")
    @PostMapping("/collect")
    @PreAuthorize("hasAuthority('repayment:overdue:collect')")
    public Result<Map<String, Object>> collectRecord(@RequestBody @Valid CollectionRecordDTO collectionDTO) {
        Map<String, Object> result = overdueService.collectRecord(collectionDTO);
        return Result.success(result);
    }

    /**
     * 获取催收记录
     */
    @Operation(summary = "获取催收记录")
    @GetMapping("/collect/{overdueId}")
    @PreAuthorize("hasAuthority('repayment:overdue:query')")
    public Result<List<LoanCollectionRecord>> getCollectionRecords(@PathVariable Long overdueId) {
        List<LoanCollectionRecord> records = overdueService.getCollectionRecords(overdueId);
        return Result.success(records);
    }

    /**
     * 逾期还款
     */
    @Operation(summary = "逾期还款")
    @PostMapping("/repay")
    @PreAuthorize("hasAuthority('repayment:overdue:repay')")
    public Result<Map<String, Object>> overdueRepay(@RequestBody @Valid OverdueRepayDTO overdueRepayDTO) {
        Map<String, Object> result = overdueService.overdueRepay(overdueRepayDTO);
        return Result.success(result);
    }

    /**
     * 手动检查并生成逾期记录
     */
    @Operation(summary = "手动检查并生成逾期记录")
    @PostMapping("/check")
    @PreAuthorize("hasAuthority('repayment:overdue:admin')")
    public Result<Integer> checkAndGenerateOverdueRecords() {
        int count = overdueService.checkAndGenerateOverdueRecords();
        return Result.success(count);
    }

    /**
     * 手动更新罚息
     */
    @Operation(summary = "手动更新罚息")
    @PostMapping("/update-penalty")
    @PreAuthorize("hasAuthority('repayment:overdue:admin')")
    public Result<Integer> updatePenaltyAmount() {
        int count = overdueService.updatePenaltyAmount();
        return Result.success(count);
    }

    /**
     * 获取逾期统计数据
     */
    @Operation(summary = "获取逾期统计数据")
    @GetMapping("/statistics")
    @PreAuthorize("hasAuthority('repayment:overdue:statistics')")
    public Result<Map<String, Object>> getStatistics(
            @Parameter(description = "开始日期") @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @Parameter(description = "结束日期") @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        Map<String, Object> statistics = overdueService.getOverdueStatistics(startDate, endDate);
        return Result.success(statistics);
    }
} 
